package com.ghostj.server;

import com.ghostj.util.Out;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class AcceptMaster extends Thread{
	//boolean acceptable=true;

	public static ArrayList<HandleMaster> masters=new ArrayList<>();
	@Override
	public void run() {
		try {
			ServerSocket serverSocket=new ServerSocket(ServerMain.masterPort);
			while (true) {
				Socket socket=serverSocket.accept();
				Out.say("AcceptMaster","正在接受Master的连接");
				HandleMaster handleMaster=new HandleMaster(socket);
				handleMaster.bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream(),"GBK"));
				handleMaster.outputStreamWriter=new OutputStreamWriter(socket.getOutputStream(),"GBK");
				masters.add(handleMaster);
				handleMaster.start();
			}
		}catch (Exception e){
			Out.say("AcceptMaster","建立与Master的连接失败");
			e.printStackTrace();
		}
	}
	public static boolean masterOnline(){
		return masters.size()!=0;
	}
}
